from zhipuai import ZhipuAI
import json
client = ZhipuAI(api_key="63811a77c983e58eab459a62ad58cc5d.M4NRNcYoGoWU5jfd")
def  ai_generated_score(original_text, summary_text):
    # 构建评分请求
    prompt = f"Here is a conversation between a user and an agent({original_text}).Now we've used an AI assistant to generate a summary of the conversation, which looks like this: {summary_text}.This is what we want to ask for your feedback on how Al Assistant is performing. Please rate the usefulness, relevance, accuracy, and level of detail of the generated summary. Each assistant receives an overall score on a scale of 1 to 10, with higher scores indicating better overall performance. Start by outputting a row with only one value that represents the assistant's overall score. In the next line, explain your assessment in terms of usefulness, relevance, accuracy, and level of detail, avoiding any potential bias, and ensuring that the order in which your responses are presented does not affect your judgment."
    
    try:
        response = client.chat.completions.create(
            model="glm-4-plus",  
            messages=[
                {"role": "user", "content": prompt}
            ]
        )
        response_str = response.to_json()
        response_json = json.loads(response_str)
        # 确保返回的数据是预期的格式
        if 'choices' in response_json:
            evaluation = response_json['choices'][0]['message']['content']
            return evaluation
        else:
            return "Unexpected response format: 'choices' not found."
    
    except Exception as e:
        return f"An error occurred: {str(e)}"


